//导入路由函数
import {createRouter,createWebHashHistory} from 'vue-router'


//定义路由数组
const routers=[
    {
        path:'/',
        name:'登录',
        component:()=>import("../views/login/index.vue")
    },
    {
        path:'/home',
        name:'首页',
        component:()=>import("../views/index/index.vue"),
        //登录权限校验
        meta:{
            requireAuth:true
        },

        children:[


            {
                path:'',
                meta: {
                    title: '系统管理'
                },
                children:[
                    {path:'/user',
                        component:()=>import("../views/user/index.vue"),
                        meta: {
                            title: '用户管理'
                        }
                    }
                ]
            }
        ]}]
//创建路由实例
        const router=createRouter({
            history:createWebHashHistory(),
            routes:routers
        })
//全局路由导航守卫.....拦截器
router.beforeEach((to, from, next) => {
    const requireAuth=to.matched.some(record=>record.meta.requireAuth)
    //检查用户是否登录
    const isLogin=!!localStorage.getItem('user')
    if(requireAuth&&!isLogin){
        next('/')
    }
    else{
        next()//放行
    }
})



//导出路由
        export default router